//------------------------------------------------------------------------------
// Copyright (C) 2007-2008 Samuel Lacroix
// This file is part of Visual Local History 2005
// 
// Visual Local History 2005 is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the License,
// or (at your option) any later version.
// 
// Visual Local History 2005 is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Visual Local History 2005; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//------------------------------------------------------------------------------

#pragma once
#include "Connect.h"


namespace HistoryAddIn
{
    ref class Config
    {
    public:
        property String^ Location
        {
            String^ get() { return location; }
        }
        property String^ IgnoredExts
        {
            String^ get() { return ignoredExts; }
            void set(String^ v) 
            { 
                ignoredExts = v;
                ignoredExtsRegex = ignoredExts->Split(';');
                for (int i = 0; i < ignoredExtsRegex->Length; ++i)
                {   
                    String^ s = ignoredExtsRegex[i]->Trim();    
                    s = s->Replace(".", "\\."); 
                    s = s->Replace("$", "\\$"); 
                    s = s->Replace("^", "\\^");
                    s = s->Replace("{", "\\{");
                    s = s->Replace("[", "\\[");
                    s = s->Replace("(", "\\(");
                    s = s->Replace("|", "\\|");
                    s = s->Replace(")", "\\)");
                    s = s->Replace("+", "\\+");
                    s = s->Replace("*", ".*");
                    s = s->Replace("?", ".");
                    ignoredExtsRegex[i] = s;
                }
            }
        }
        property array<String^>^ IgnoredExtsRegex
        {
            array<String^>^ get() { return ignoredExtsRegex; }
        }
        property int RetentionTimeInDays
        {
            int get() { return retentionTimeInDays; }
            void set(int v) 
            { 
                retentionTimeInDays = v; 
            }
        }
//         property int MaxHistorySizePerFile
//         {
//             int get() { return maxHistorySizePerFile; }
//             void set(int v) 
//             { 
//                 maxHistorySizePerFile = v; 
//             }
//         }
//         property int MaxEntriesPerFile
//         {
//             int get() { return maxEntriesPerFile; }
//             void set(int v) 
//             { 
//                 maxEntriesPerFile = v; 
//             }
//         }
        property int MinTimeBetweenBackups
        {
            int get() { return minTimeBetweenBackups; }
            void set(int v) 
            { 
                minTimeBetweenBackups = v; 
            }
        }
        
        property String^ DiffToolCommand
        {
            String^ get() { return diffToolCommand; }
            void set(String^ v) 
            { 
                diffToolCommand = v; 
            }
        }
        
        property String^ DiffToolArgs
        {
            String^ get() { return diffToolArgs; }
            void set(String^ v) 
            { 
                diffToolArgs = v; 
            }
        }
        
        property bool NewVersionCheck
        {
            bool get() { return newVersionCheck; }
            void set(bool v) 
            { 
                newVersionCheck = v; 
            }
        }

        property bool Visible
        {
            bool get() { return visible; }
            void set(bool v) 
            { 
                visible = v; 
            }
        }

        Config(DTE2 ^ applicationObject) :
        applicationObject(applicationObject)
        {
            /*
            String^ Location;
            String^ SubSubKey = applicationObject->Version[0] == '9' ? "9.0" : "8.0";
            RegistryKey^ key = Registry::CurrentUser->OpenSubKey("SOFTWARE\\Microsoft\\VisualStudio\\" + SubSubKey);
            if (key)
            {
                Location = System::IO::Path::Combine(Environment::ExpandEnvironmentVariables((String^) key->GetValue("VisualStudioLocation")), "Addins");
            }
            if (!Location)
            {
                // TODO
            }
            */
            
            config = System::Configuration::ConfigurationManager::OpenExeConfiguration(System::Reflection::Assembly::GetExecutingAssembly()->Location);
            // config = System::Configuration::ConfigurationManager::OpenExeConfiguration(IO::Path::Combine(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "VLH2005\\user.exe"));
            

            location = config->FilePath;                                                  
            
            if ((settings = (System::Configuration::AppSettingsSection^) config->Sections["settings"]) == nullptr)
            {
                settings = gcnew System::Configuration::AppSettingsSection;
                settings->SectionInformation->AllowExeDefinition = System::Configuration::ConfigurationAllowExeDefinition::MachineToLocalUser;
                config->Sections->Add("settings", settings);
                config->Save();
            }

            IgnoredExts = gcnew String("");
            RetentionTimeInDays = 0;
//             MaxHistorySizePerFile = 50;
//             MaxEntriesPerFile = 100;
            MinTimeBetweenBackups = 0;
            diffToolCommand = gcnew String("");
            diffToolArgs = gcnew String("");
            newVersionCheck = true;
        }
        
        void SaveToAppConfig()
        {
            System::Configuration::KeyValueConfigurationElement^ pConfig;
            
            pConfig = settings->Settings["IgnoredExts"];
            if (!pConfig) 
            {
                    settings->Settings->Add("IgnoredExts", "");
                    pConfig = settings->Settings["IgnoredExts"];
            }
            pConfig->Value = ignoredExts;
            
            pConfig = settings->Settings["RetentionTimeInDays"];
            if (!pConfig) 
            {
                    settings->Settings->Add("RetentionTimeInDays", "");
                    pConfig = settings->Settings["RetentionTimeInDays"];
            }
            pConfig->Value = retentionTimeInDays.ToString();
            
//             pConfig = settings->Settings["MaxHistorySizePerFile"];
//             if (!pConfig) 
//             {
//                     settings->Settings->Add("MaxHistorySizePerFile", "");
//                     pConfig = settings->Settings["MaxHistorySizePerFile"];
//             }
//             pConfig->Value = maxHistorySizePerFile.ToString();
//             
//             pConfig = settings->Settings["MaxEntriesPerFile"];
//             if (!pConfig) 
//             {
//                     settings->Settings->Add("MaxEntriesPerFile", "");
//                     pConfig = settings->Settings["MaxEntriesPerFile"];
//             }
//             pConfig->Value = maxEntriesPerFile.ToString();
//             
            pConfig = settings->Settings["MinTimeBetweenBackups"];
            if (!pConfig) 
            {
                    settings->Settings->Add("MinTimeBetweenBackups", "");
                    pConfig = settings->Settings["MinTimeBetweenBackups"];
            }
            pConfig->Value = minTimeBetweenBackups.ToString();
            
            pConfig = settings->Settings["DiffToolCommand"];
            if (!pConfig) 
            {
                    settings->Settings->Add("DiffToolCommand", "");
                    pConfig = settings->Settings["DiffToolCommand"];
            }
            pConfig->Value = diffToolCommand;
            
            pConfig = settings->Settings["DiffToolArgs"];
            if (!pConfig) 
            {
                    settings->Settings->Add("DiffToolArgs", "");
                    pConfig = settings->Settings["DiffToolArgs"];
            }
            pConfig->Value = diffToolArgs;
            
            pConfig = settings->Settings["NewVersionCheck"];
            if (!pConfig) 
            {
                settings->Settings->Add("NewVersionCheck", "");
                pConfig = settings->Settings["NewVersionCheck"];
            }
            pConfig->Value = newVersionCheck.ToString();

            pConfig = settings->Settings["visible"];
            if (!pConfig) 
            {
                    settings->Settings->Add("visible", "");
                    pConfig = settings->Settings["visible"];
            }
            pConfig->Value = visible.ToString();

            
            config->Save();
            System::Configuration::ConfigurationManager::RefreshSection("appSettings");
        }
        
        void LoadFromAppConfig()
        {
            System::Configuration::KeyValueConfigurationElement^ pConfig; 
            
            pConfig = settings->Settings["IgnoredExts"];
            if (pConfig) IgnoredExts = pConfig->Value;

            pConfig = settings->Settings["RetentionTimeInDays"];
            if (pConfig) RetentionTimeInDays = Convert::ToInt32(pConfig->Value);

//             pConfig = settings->Settings["MaxHistorySizePerFile"];
//             if (pConfig) maxHistorySizePerFile = Convert::ToInt32(pConfig->Value);
// 
//             pConfig = settings->Settings["MaxEntriesPerFile"];
//             if (pConfig) maxEntriesPerFile = Convert::ToInt32(pConfig->Value);

            pConfig = settings->Settings["MinTimeBetweenBackups"];
            if (pConfig) MinTimeBetweenBackups = Convert::ToInt32(pConfig->Value);
            
            pConfig = settings->Settings["DiffToolCommand"];
            if (pConfig) DiffToolCommand = pConfig->Value;

            pConfig = settings->Settings["DiffToolArgs"];
            if (pConfig) DiffToolArgs = pConfig->Value;

            pConfig = settings->Settings["NewVersionCheck"];
            if (pConfig) NewVersionCheck = Convert::ToBoolean(pConfig->Value);

            pConfig = settings->Settings["visible"];
            if (pConfig) Visible = Convert::ToBoolean(pConfig->Value);
        }
        
        
    private:
        String^ location;
        DTE2^ applicationObject;
        String^ ignoredExts;                // list of extensions to ignore
        array<String^>^ ignoredExtsRegex;   // ignoredExts patterns in regex format
        int retentionTimeInDays;            // number of days to keep a backup in the history
//         int maxHistorySizePerFile;          // maximum history size per file (in MB)
//         int maxEntriesPerFile;              // maximum number of entries per file
        int minTimeBetweenBackups;          // in seconds
        String^ diffToolCommand;
        String^ diffToolArgs;
        bool newVersionCheck;
        bool visible;
        
        System::Configuration::Configuration^ config;
        System::Configuration::AppSettingsSection^ settings;
    };
    
}
